package utgame.com.fakelegendary.activity;


import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import utgame.UtMessage;
import utgame.com.fakelegendary.R;


import utgame.UtMessage.MessagePacket.MessageType;
import utgame.com.fakelegendary.socket.ServerSession;

public class MainActivity extends Activity implements OnClickListener{

    public static final String TAG = "MainActivity";

    private Button btn_save, btn_resume;
    private ImageView iv_canvas;
    private Bitmap baseBitmap;
    private Canvas canvas;
    private Paint paint;

    private Bitmap bmpHero;


    private float lastX = 50;
    private float lastY = 50;

    private Button mBtnControl;
    private GameView mGameView;

    private RemoteSurfaceView mRemoteSurfaceView;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

                case MessageType.PlayerLogoff_VALUE:
                    mGameView.invalidate();
                    break;
                case MessageType.PlayerActionMove_VALUE:
                    mGameView.invalidate();
                    break;
                case MessageType.PlayerActionEatBall_VALUE:
                    mGameView.invalidate();
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化一个画笔，笔触宽度为5，颜色为红色
//        paint = new Paint();
//        paint.setStrokeWidth(5);
//        paint.setColor(Color.RED);

//        iv_canvas = (ImageView) findViewById(R.id.iv_canvas);
//        btn_save = (Button) findViewById(R.id.btn_save);
//        btn_resume = (Button) findViewById(R.id.btn_resume);

//        btn_save.setOnClickListener(click);
//        btn_resume.setOnClickListener(click);
//        iv_canvas.setOnTouchListener(touch);

        //画图片，就是贴图
//        bmpHero = BitmapFactory.decodeResource(getResources(), R.drawable.hero);

        mBtnControl = (Button) findViewById(R.id.btn_control);
        mBtnControl.setOnClickListener(this);

        WindowManager wm = this.getWindowManager();

        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        LinearLayout layout = (LinearLayout) findViewById(R.id.ll_main);


//        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.dance_relative_layout);
        mRemoteSurfaceView = new RemoteSurfaceView(this);
//        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,  RelativeLayout.LayoutParams.MATCH_PARENT);
//        mRemoteSurfaceView.setLayoutParams(params);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400, Gravity.TOP);
        params.setMargins(0, 0, 0, 0);

        mRemoteSurfaceView.setLayoutParams(params);


        layout.addView(mRemoteSurfaceView);


        mGameView = new GameView(this);
        mGameView.setMinimumHeight(height);
        mGameView.setMinimumWidth(width);
        mGameView.invalidate();

        layout.addView(mGameView);
//
//        ServerSession.getInstance().addHandler(TAG, mHandler);





    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_control:
                mGameView.setIsDrawHero(!mGameView.getIsDrawHero());
                mGameView.invalidate();
                break;
        }
    }
//
//    private View.OnTouchListener touch = new OnTouchListener() {
//        // 定义手指开始触摸的坐标
//        float startX;
//        float startY;
//
//        @Override
//        public boolean onTouch(View v, MotionEvent event) {
//            switch (event.getAction()) {
//                // 用户按下动作
//                case MotionEvent.ACTION_DOWN:
//                    // 第一次绘图初始化内存图片，指定背景为白色
//                    if (baseBitmap == null) {
//                        baseBitmap = Bitmap.createBitmap(iv_canvas.getWidth(),
//                                iv_canvas.getHeight(), Bitmap.Config.ARGB_8888);
//                        canvas = new Canvas(baseBitmap);
//                        canvas.drawColor(Color.WHITE);
//
//
//                        canvas.drawBitmap(bmpHero, lastX, lastY, paint);
//                    }
//                    // 记录开始触摸的点的坐标
//                    startX = event.getX();
//                    startY = event.getY();
//                    break;
//                // 用户手指在屏幕上移动的动作
//                case MotionEvent.ACTION_MOVE: {
//                    // 记录移动位置的点的坐标                    float stopX = event.getX();
//                    float stopY = event.getY();
//                    float stopX = event.getX();
//                    //根据两点坐标，绘制连线
////                    canvas.drawLine(startX, startY, stopX, stopY, paint);
//
//
//                    float k = (stopY - lastY) / (stopX - lastX);
//                    lastX = lastX + stepX * k;
//                    lastY = lastY + stepY * k;
//
//                    canvas.drawBitmap(bmpHero, lastY, lastY, paint);
//
//
//                    // 更新开始点的位置
//                    startX = event.getX();
//                    startY = event.getY();
//
//                    // 把图片展示到ImageView中
//                    iv_canvas.setImageBitmap(baseBitmap);
////
//                    break;
//                }
//                case MotionEvent.ACTION_UP: {
//
////                    float stopX = event.getX();
////                    float stopY = event.getY();
////
////                    //根据两点坐标，绘制连线
//////                    canvas.drawLine(startX, startY, stopX, stopY, paint);
////
////                    float k = (stopY - lastY) / (stopX - lastX);
////                    lastX = lastX + stepX * k;
////                    lastY = lastY + stepY * k;
////
////                    canvas.drawBitmap(bmpHero, lastX, lastY, paint);
//
//                    break;
//                }
//                default:
//                    break;
//            }
//            return true;
//        }
//    };
//    private View.OnClickListener click = new OnClickListener() {
//
//        @Override
//        public void onClick(View v) {
//
//            switch (v.getId()) {
//                case R.id.btn_resume:
//                    resumeCanvas();
//                    break;
//                default:
//                    break;
//            }
//        }
//    };
//
//    /**
//     * 清除画板
//     */
//    protected void resumeCanvas() {
//        // 手动清除画板的绘图，重新创建一个画板
//        if (baseBitmap != null) {
//            baseBitmap = Bitmap.createBitmap(iv_canvas.getWidth(),
//                    iv_canvas.getHeight(), Bitmap.Config.ARGB_8888);
//            canvas = new Canvas(baseBitmap);
//            canvas.drawColor(Color.WHITE);
//            iv_canvas.setImageBitmap(baseBitmap);
//        }
//    }


    public GameView getmGameView(){return mGameView;}
}